import { PartialType } from '@nestjs/mapped-types';
import { Transform } from 'class-transformer';
import { IsBoolean, IsInt, IsNotEmpty, IsNumber, IsOptional, IsString, MaxLength } from 'class-validator';
import { PagingDto } from 'src/common/dto';

export class CreateProductDto {
  @IsNotEmpty({ message: '商品名不得为空' })
  @MaxLength(200, { message: '商品名最长不得超过200' })
  @IsString()
  name: string;

  @IsNotEmpty({ message: '商品价格不得为空' })
  @Transform(({ value }) => parseFloat(value))
  @IsNumber()
  price: number;

  @IsNotEmpty({ message: '商品描述不得为空' })
  @IsString()
  description: string;

  @IsOptional({ always: true })
  @IsString()
  mainImage?: string;

  @IsOptional({ always: true })
  @IsString({ each: true })
  carouselImages?: string[];

  @IsOptional()
  @IsString()
  detailHtml?: string;
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;

  @IsNotEmpty({ message: '分类id不得为空' })
  @Transform(({ value }) => parseInt(value, 10))
  @IsNumber()
  categoryId: number;
}

export class UpdateProductDto extends PartialType(CreateProductDto) {
  @IsNotEmpty({ message: 'id不得为空' })
  @Transform(({ value }) => parseInt(value, 10))
  @IsInt()
  id: number;
}

export class QueryProductDto extends PagingDto {
  @IsOptional({ always: true })
  name?: string;

  @IsOptional({ always: true })
  @IsInt()
  categoryId?: number;
}
